package com.ol.edu.server.service.user.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ol.edu.server.entity.*;
import com.ol.edu.server.mapper.*;
import com.ol.edu.server.service.user.UserService;
import com.ol.edu.server.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Roger
 * @date 2020/9/7 17:12
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    /*
        分页获取用户数据&条件查询用户数据
     */
    @Override
    public PageInfo<User> findAllUserByPage(UserVo userVo) {
        // 获取分页数据
        PageHelper.startPage(userVo.getCurrentPage(), userVo.getPageSize());
        List<User> users = userMapper.findAllUserByPage(userVo);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }
    /*
        修改用户状态
     */
    @Override
    public Map updateUserStatus(User user) {
        user.setUpdateTime(new Date());
        Integer res = userMapper.updateUserStatus(user);
        //如果res返回1 表示更新成功
        if (res == 1) {
            Map<String, String> map = new HashMap(1);

            map.put("status", (user.getStatus().equals("ENABLE") ? "DISABLE" : "ENABLE"));
            return map;
        }
        return null;
    }
    /*
        用户登录
     */
    @Override
    public User login(User user) {
        User user1 = userMapper.login(user);
        if (user1 != null && Md5.verify(user.getPassword(), user1.getPassword())) {
            return user1;
        } else {
            return null;
        }
    }

    @Autowired
    private RoleMapper roleMapper;
    /*
       获取用户拥有的菜单权限
    */
    @Override
    public List<Role> findUserRoleById(Integer id) {
        List<Role> roleList = roleMapper.findUserRoleById(id);
        return roleList;
    }

    @Autowired
    private UserRoleMapper userRoleMapper;
    /*
        分配角色
     */
    @Override
    public void userContextRole(UserVo userVo) {
        Integer userId = userVo.getUserId();
        //删除user_role_relation表原来的关系
        userRoleMapper.deleteByUserId(userId);
        List<Integer> roleIdList = userVo.getRoleIdList();
        for (Integer roleId : roleIdList) {
            //插入数据
            userRoleMapper.insert(userId, roleId);
        }
    }

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    /*
        获取用户拥有的菜单权限
     */
    @Override
    public ResponseResult getUserPermissions(Integer userId) {
        //获取该ID用户拥有的父菜单数据
        List<Menu> parentMenuList = menuMapper.getParentMenuInfoByUserId(userId);
        //根据父菜单获取到对应的子菜单
        for (Menu parentMent : parentMenuList) {
             List<Menu> subMenuList = menuMapper.getSubMenu(parentMent.getId());
             parentMent.setSubMenuList(subMenuList);
        }
        //获取该ID拥有的资源数据
        List<Resource> resourceList = resourceMapper.getResourceInfoByUserId(userId);
        //封装数据返回给前端
        Map<String, Object> map = new HashMap<>(2);
        map.put("menuList", parentMenuList);
        map.put("resourceList", resourceList);

        return new ResponseResult(true, 200, "获取用户拥有的权限成功", map);
    }


}
